
/**
孵化状态
 *
*/
 
import { SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { role_prefix } from "utils/constants/bodyconstant";
import { generateUniqueId } from "utils/utils";

export class SpawnState<T extends IStateArg<SpawnEntity >> extends State<T>
{
    // 可以通过孵化需要的时间。来决定下一次启用孵化的时间，尽量少用isActive()开销过高
    public getActions(arg:T):Action[]
    {
    
        arg.entity.checkExpireTask();
        let nextSpawnTask =  arg.entity.spawnQueue.top();
        if(!nextSpawnTask)
        {
            return []
        }
       
        // if(1)
        // return [] 
       // arg.entity.log("info",`spawnActiveState 孵化任务队列 ${JSON.stringify(_.sortBy(arg.entity.spawnQueue.values(),e=>-e.priority).map(e=>{return {role:e.data.spawn_info[0].bodyconfig.Role,priority:e.data.priority}}))}`);
        // 孵化完成后。注册creep
       // arg.entity.log("info",`当前孵化任务 ${JSON.stringify(nextSpawnTask)}`);
        for(const e of arg.entity.spawnStructures())
        {
            const spawn = e;
            if(!spawn.spawning)
            {
                const actionarg = this.getSpawnAction(arg,spawn,nextSpawnTask);
                if(actionarg)
                {
                    const action:IActionCMD={
                        arg:actionarg,
                        key:"spawn",
                        priority:1,
                        absort:0,
                        ttl:-1,
                        onError:()=>{
                            // Memory.Debug[`孵化错误:${arg.entity.GUID()}_${Game.time}_${actionarg.creepName}`]=JSON.stringify(arg.entity.spawnQueue.top()?.data.spawn_info[0].creepName);
                            // arg.entity.spawnQueue.pop();
                        },
                    }
                    return [action];
                }
                else 
                {
                    // renew 
                }
            }
            else
            {
                if(spawn.spawning && spawn.spawning.remainingTime === 0)
                {
                    arg.entity.makeSpaceForSpawn(spawn);
                }
            }
        }
        return [];
    }

    private getSpawnAction(arg:T,spawn:StructureSpawn,task:ITask<ISpawnData>)
    {
        for(const spawninfo of task.data.spawn_info)
        {
            if(!spawninfo.creepName)
            {
                
                const names:string[] =[]
                for(let i=0;i<spawninfo.count;i++)
                {
                    names.push(`${role_prefix[spawninfo.bodyconfig.Role]}${generateUniqueId()}`)
                }
                spawninfo.creepName = names
            }
            if(spawninfo.creepName!.length ==0)
            {
                // Memory.Debug[`${arg.entity.GUID()}_孵化错误：SpawnInfo Name 已经为空_${Game.time}`]=`${JSON.stringify(spawninfo.creepName)}-count:[${spawninfo.count}]`;
                return undefined;
            }
            const action:ISpawnActionArg<SpawnEntity>={
                entity:arg.entity,
                bodycfg:spawninfo.bodyconfig,        // 角色
                corssLevel:0,  // 对穿等级
                roomName:spawn.room.name,    // 孵化后的爬归属于哪一个房间
                creepName:spawninfo.creepName[0],   // 要孵化的爬虫名称
                spawnName:spawn.name,   // 孵化的Spawn名称
                teamid:task.data.teamid
                // boost:spawninfo.boost,       // boost等级
            };
            
            return action;
        }
        return undefined;
    }
}
